package{
    import flash.display.Sprite;
    import flash.text.TextField;
    import fr.mk25.audio.*;    
    import flash.net.*;
    import flash.media.*;
    import flash.events.*;
    import flash.display.Stage;
    import flash.display.Shape;
    import flash.display.Graphics;
    import flash.ui.Keyboard;

    import flash.system.System;

    public class M3UPlaylistTest extends Sprite {

        private var author:TextField;
        private var volume:TextField;
        private var song:TextField;
        private var playlist:M3UPlaylist;
        private var status:String;

        private const shapeWidth:int = 100;
        private const shapeHeight:int = 6;
        


        public function M3UPlaylistTest() {

            
            var memory_load:TextField = new TextField();
            memory_load.x = 0;
            memory_load.y = 0;
            memory_load.width = 300;
            memory_load.text = "Memory used: undefined";

            addEventListener(Event.ENTER_FRAME, _onEnterFrame);
            addChild(memory_load);

            status = "none";


            var bgShape:Shape = new Shape();
            var progressShape:Shape = new Shape();
         
            bgShape.x = 100;
            bgShape.y = 200;
            progressShape.x = 100;
            progressShape.y = 200;

            var bgGraphics:Graphics = bgShape.graphics;

            bgGraphics.beginFill(0xCCCCCC);
            bgGraphics.lineTo(shapeWidth+1, 1);
            bgGraphics.lineTo(shapeWidth+1, -shapeHeight-1);
            bgGraphics.lineTo(-1, -shapeHeight-1);
            bgGraphics.lineTo(-1, 1);
        
            bgGraphics.endFill();


            addChild(bgShape);
            addChild(progressShape);
            song = new TextField();
            author = new TextField();
 
            author.x = 100;
            author.y = 100;
            author.width = 300;
            author.text = "Author undefined";
            addChild(author);
    
            song.x = 100;
            song.y = 120;
            song.width = 300;
            song.text = "Song name undefined";
            addChild(song);

            playlist = new M3UPlaylist("playlist.m3u")

            playlist.addEventListener(PlaylistEvent.ID3, reloadID3Info); 
            playlist.addEventListener(PlaylistEvent.PLAYING_NEW_TRACK, reloadFileInfo); 
            playlist.addEventListener(PlaylistEvent.LOADED, onLoaded); 

            this.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler); 

            volume = new TextField();
            volume.x = 100;
            volume.y = 50;
            volume.text="Volume: undefined";
            addChild(volume);

            function _onEnterFrame(e:Event):void {

               
                if(status=="playing"){
                    var g:Graphics = progressShape.graphics;

                    // The following command qpends a lot of memory    
                    g.clear();    
    
                    g.beginFill(255);
                    g.lineTo(playlist.getProgressAsPercentage()*shapeWidth, 0);
                    g.lineTo(playlist.getProgressAsPercentage()*shapeWidth, -shapeHeight);
                    g.lineTo(0, -shapeHeight);
                    g.lineTo(0, 0);
        
                    g.endFill();
                }


                volume.text="Volume: " + playlist.getVolume().toString() ;

                var memoryBytes:Number = System.totalMemory/1048576;               
                memory_load.text = "Memory used: " + memoryBytes.toString() +"MBytes";
            }

       }
        private function keyDownHandler(event : KeyboardEvent) : void {     
            if (event.keyCode == Keyboard.DOWN){
                playlist.pause();
            }
            if (event.keyCode == Keyboard.UP){
                playlist.play();
            }

            if (event.keyCode == Keyboard.RIGHT){
                playlist.playNext();
            }
            if (event.keyCode == Keyboard.LEFT){
                playlist.playPrevious();
            }

            if (event.keyCode == Keyboard.NUMPAD_2){
                playlist.setVolume( playlist.getVolume()-10 );
            }
            if (event.keyCode == Keyboard.NUMPAD_8){
                playlist.setVolume( playlist.getVolume()+10 );
            }
    
        }
        private function reloadFileInfo(event : Event) : void {     
            song.text = "Reading ID3 informations";
            author.text = "";
        }
        private function onLoaded(event : Event) : void {     
            trace("Playlist loaded");
            playlist.play(); 
            status="playing";
        }
        private function reloadID3Info(event : Event) : void {     
            try{
                var id3:ID3Info = playlist.getTrackID3();
                song.text = id3.songName;
                author.text = id3.artist;
            }catch(error:TypeError){
                song.text   = "?????";
                author.text = "?????";
            }
        }
    }
}
